package com.hzhr.iot.device.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.device.sdk.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.sdk.model.nbMeter_sell.NbMeter_SellGetModel;
import com.hzhr.iot.device.sdk.model.nbMeter_sell.NbMeter_SellPostModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Device_NbMeter_SellService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 销售NB表 <br/><br/>
     * 1. 获取NB表：查询登录用户所在公司下的NB表物联台账 <br/>
     * 2, 验证NB表是否具备销售条件：如果该NB表的物联网台账有所属的燃气公司，或则没有代码，则不具备销售条件 <br/>
     * 3. 验证购买公司的合法性：查询购买公司，如果购买公司不存在或者不是燃气公司，则不合法 <br/>
     * 4. 将NB物联表分配到购买公司的顶级部门下：修改NB表的物联台账，将其分配到购买公司的顶级部门下 <br/>
     * 5. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * NBMETER_SELLSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 获取NB表<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表不存在<br/>
     * NBMETER_SELLSERVICE_POST_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证购买公司的合法性<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 购买公司不合法<br/>
     * NBMETER_SELLSERVICE_POST_203 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB表是否具备销售条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表已经销售，无法继续销售<br/>
     * NBMETER_SELLSERVICE_POST_204 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB表是否具备销售条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表没有代码，无法销售<br/>
     * @see NbMeter_SellPostModel body <br/>
     * @see NbMeter_SellGetModel result <br/>
     */ 
    public NbMeter_SellGetModel post(Map<String, String> header, Integer nbMeterId, Map<String, Object> query, NbMeter_SellPostModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("nbMeterId", nbMeterId);

        try {
            ResponseEntity<NbMeter_SellGetModel> result = restTemplate.exchange("http://device-service/nb-meters/{nbMeterId}/sell?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.POST, httpEntity, NbMeter_SellGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("NBMETER_SELLSERVICE_POST_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("NBMETER_SELLSERVICE_POST_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

